package com.techysky.controller;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techysky.annoation.Operation;
import com.techysky.entity.CommentTags;
import com.techysky.entity.DataModule;
import com.techysky.entity.MaintPlan;
import com.techysky.entity.Marker;
import com.techysky.entity.Note;
import com.techysky.entity.SysLog;
import com.techysky.entity.User;
import com.techysky.service.AdaptService;
import com.techysky.service.CommentTagsService;
import com.techysky.service.DataModuleService;
import com.techysky.service.MaintPlanService;
import com.techysky.service.MarkerService;
import com.techysky.service.MultiMediaService;
import com.techysky.service.NoteService;
import com.techysky.service.ProjectService;
import com.techysky.service.PublicModuleService;
import com.techysky.service.SysLogService;

@Controller
@RequestMapping("/ietm")
public class IetmController implements Serializable {

	
	private static final String COMMENT = "comment";
	private static final String PROJECT_ID = "projectId";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private AdaptService adaptService;
	@Autowired
	private MaintPlanService maintPlanService;	
	@Autowired
	private DataModuleService dataModuleService;
	@Autowired
	private MultiMediaService multiMediaService;
	@Autowired
	private transient CommentTagsService commentTagsService;
	@Autowired
	private PublicModuleService publicModuleService;
	@Autowired
	private MarkerService markerService;
	@Autowired
	private NoteService noteService;
	@Autowired
	private ProjectService projectService;
	
	
	/**
	 * 获取IETM列表展示数据
	 * 1、第一级，主页面只有第一级数据
	 * 2、第二级包含第一级和第二第三级
	 * 3、第三级 文件夹和文件
	 * @param type pm dm
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/data/{pid}")
	@ResponseBody
	public Object getIetm(@PathVariable("pid")String pid,String type) {
		if(!StringUtils.isEmpty(type)) {
			return this.publicModuleService.findByParentIdAndType(type,pid);
		}else {
			return this.publicModuleService.findByParentId(pid);
		}
		
	}
	
	/**
	 * 通过dm的id获取一条数据，为dm页面展示提供数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getDataModule")
	@ResponseBody
	public DataModule getDataModule(String id) {
		return this.dataModuleService.getObject(id);
	}
	
	/**
	 * 上一个下一个dm获取
	 * @param index
	 * @return
	 */
	@RequestMapping(value="/getDM")
	@ResponseBody
	public DataModule getDM(Integer index) {
		return this.dataModuleService.getDMByIndex(index);
	}
	
	
	/**
	 * 通过dmCode查询数据模块
	 * @param dmCode
	 * @return
	 */
	@RequestMapping(value="/getDmByDmCode")
	@ResponseBody
	public DataModule getDmByDmCode(String dmCode) {
		return this.dataModuleService.findByDmCode(dmCode);
	}
	
	/**
	 * 适用性数据解析
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/adapt")
	@ResponseBody
	public Object adapt(String name) {
		return adaptService.findByNameLike(name);
	}
	
	/**
	 * 书签保存
	 * @param marker
	 * @return
	 */
	@RequestMapping(value="/saveMarker")
	@ResponseBody
	public Object saveMarker(Marker marker) {
		return this.markerService.saveOrUpdate(marker);
	}
	
	/**
	 * 查询书签列表
	 * @param titile
	 * @return
	 */
	@RequestMapping(value="/findByTitle")
	@ResponseBody
	public Object findByTitle(String title) {
		return this.markerService.findByTitleLike(title);
	}
	
	@RequestMapping(value="/findById")
	@ResponseBody
	public Object findById(String id) {
		return this.markerService.findById(id);
	}
	
	/**
	 * 注释保存
	 * @param marker
	 * @return
	 */
	@RequestMapping(value="/saveNote")
	@ResponseBody
	public Object saveNote(Note note) {
		return this.noteService.saveOrUpdate(note);
	}
	
	/**
	 * 注释删除
	 * @param marker
	 * @return
	 */
	@RequestMapping(value="/delNote")
	@ResponseBody
	public Object delNote(String id) {
		return this.noteService.delete(id);
	}
	
	
	/**
	 * 查询注释列表
	 * @param titile
	 * @return
	 */
	@RequestMapping(value="/findByContent")
	@ResponseBody
	public Object findByContent(String content) {
		return this.noteService.findByContent(content);
	}
	

	
	
	/**
	 * 通过dmCode查询注释列表
	 * @param dmCode
	 * @return
	 */
	@RequestMapping(value="/findByDmCode")
	@ResponseBody
	public Object findByDmCode(String dmCode) {
		return this.noteService.findByDmCode(dmCode);
	}
	
	
	/**
	 * 用户帮助
	 * @param dm
	 * @return
	 */
	@RequestMapping("/user/help")
	@ResponseBody
    public Object userHelp(DataModule dm) {
    	return this.dataModuleService.query(dm);
    }
	
	/**
	 * 用户日志
	 * @param log
	 * @return
	 */
	@RequestMapping("/log")
	@ResponseBody
	public Object log(SysLog log) {
		return this.sysLogService.query(log);
	}
	//用户帮助-搜索范围-pm列表
/*	@ResponseBody
	@RequestMapping("/getProjectId")
	public Object getProjectId() {
		return this.projectService.getProjects();
	}*/
	@ResponseBody
	@RequestMapping("/getProjects")
	public Object getProjects() {
		return this.projectService.getProjects();
	}
	//用户帮助-搜索范围-型号识别码
	@ResponseBody
	@RequestMapping("/getModelIdentCodeList")
	public Object getModelIdentCodeList(DataModule dm) {
		String pmcode=dm.getPmCode();
		return this.dataModuleService.getModelIdentCodeList(pmcode);
	}
	//用户帮助-搜索范围-系统
	@ResponseBody
	@RequestMapping("/getSystemCodeList")
	public Object getSystemCodeList(DataModule dm) {
		return this.dataModuleService.getSystemCodeList(dm.getPmCode(),dm.getModelIdentCode());
	}
	//用户帮助-搜索范围-分系统
	@ResponseBody
	@RequestMapping("/getSubSystemCodeList")
	public Object getSubSystemCodeList(DataModule dm) {
		return this.dataModuleService.getSubSystemCodeList(dm.getPmCode(),dm.getModelIdentCode(),dm.getSystemCode());
	}
	//用户帮助-搜索范围-单元
	@ResponseBody
	@RequestMapping("/getAssyCodeList")
	public Object getAssyCodeList(DataModule dm) {
		return this.dataModuleService.getAssyCodeList(dm.getPmCode(),dm.getModelIdentCode(),dm.getSystemCode(),dm.getSubSystemCode());
	}
	//用户帮助-搜索范围-信息码
	@ResponseBody
	@RequestMapping("/getInfoCodeList")
	public Object getInfoCodeList(DataModule dm) {
		return this.dataModuleService.getInfoCodeList(dm.getPmCode(),dm.getModelIdentCode(),dm.getSystemCode(),dm.getSubSystemCode(),dm.getAssyCode());
	}
	/**
	 * ietm所有页面跳转
	 * @param page
	 * @return
	 */
	@RequestMapping("/{page}")
	public String redirect(@PathVariable(value="page") String page){
		return page;
	}
	/**
	 * 日志首页面+查询
	 * @return
	 */
	//@Operation(value="日志查询",type="2")
	@RequestMapping("/ietm-log")
	public String log(SysLog log,Model model) {
		model.addAllAttributes(this.sysLogService.query(log));
		return "ietm-log";
	}
	
	/**
	 * 日志高级搜索
	 * @return
	 */
	@RequestMapping("/ietm-log-search")
	public String logSearch() {
		return "ietm-log-search";
	}
	
	/**
	 * 多媒体查询
	 * @param name
	 * @return
	 */
	@RequestMapping("/multimedia")
	@ResponseBody
	public Object multimedia(String name) {
		return this.multiMediaService.findByNameLike(name);
	}
	
	/**
	 * @param home主页的帮助
	 * @param model
	 * @return
	 */
	@RequestMapping("/ietmhelp")
	public String ietmHelp() {
		return "ietmhelp";
	}
	
	/**
	 * 维修计划首页面
	 * @return
	 */
	@RequestMapping("/maintplan")
	public String maintplan(MaintPlan maintPlan,Model model) {
		model.addAllAttributes(this.maintPlanService.query(maintPlan));
		return "maintenance-plan";
	}
	/**
	 * 注释书签保存
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/commentTags/updateAnno")
	@Operation("新增书签注释")
	@ResponseBody
	public  Object saveOrUpdateAnno(CommentTags commentTags) {
		return this.commentTagsService.updateAnnoById(commentTags);
	}
	/**
	 * 注释书签保存无需跳转页面
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/commentTags/saveOrUpdate")
	@Operation(value="新增书签注释",type="2")
	@ResponseBody
	public  Object saveOrUpdateCommentTag(HttpServletRequest request) {
		return this.commentTagsService.saveOrUpdate(request);    
	}

	/**
	 * 
	 * @描述: 注释书签保存-需跳转页面
	 * @方法名: saveOrUpdateCommentTag22
	 * @param request
	 * @return
	 * @返回类型 Object
	 * @创建人 lin
	 * @创建时间 2018年10月17日下午2:03:37
	 * @修改人 lin
	 * @修改时间 2018年10月17日下午2:03:37
	 * @修改备注 修改1.1.3 32 问题
	 * @since
	 * @throws
	 
	 */
	
	@RequestMapping("/commentTags/saveOrUpdate22")
	@Operation(value="编辑书签注释",type="2")
	public  Object saveOrUpdateCommentTag22(HttpServletRequest request) {
		this.commentTagsService.saveOrUpdate(request); 
		if("tag".equals(request.getParameter("type"))){
			return "redirect:/ietm/tags";
		}
		else
			return "redirect:/ietm/annotations";
	}
	/**
	 * 注释书签删除
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/commentTags/delete")
	@Operation(value="删除书签注释",type="2")
	@ResponseBody
	public  Object deleteCommentTags(CommentTags commentTags) {
		return this.commentTagsService.delete(commentTags.getId());
	}
	/**
	 * 注释书签删除多个
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/commentTags/deleteIds")
	@Operation(value="删除书签注释",type="2")
	@ResponseBody
	public  Object deleteCommentTagsIds(@RequestParam(value="ids")  String ids) {
		return this.commentTagsService.delete(ids);
	}
	
	/**
	 * 
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/annoations/delete")
	@Operation(value="删除注释",type="2")
	@ResponseBody
	public  Object deleteAnno(CommentTags commentTags) {
		return this.commentTagsService.delete(commentTags.getId());
	}
	
	/**
	 * 
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/annoations/delFile")
	@Operation(value="删除附件操作",type="2")
	@ResponseBody
	public Object deleteFile(CommentTags commentTags) {
		return this.commentTagsService.deleteFile(commentTags.getId());
	}
	
	/**
	 * 
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/tags/delete")
	@Operation(value="删除书签",type="2")
	@ResponseBody
	public Object deleteTags(CommentTags commentTags) {
		return this.commentTagsService.delete(commentTags.getId());
	}
	
	/**
	 * 注释书签查询
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/annotations")
	public String queryAnno(CommentTags commentTags,Model model,HttpServletRequest request) {
		commentTags.setOperation(COMMENT);
		commentTags.setUserName(request.getSession().getAttribute("user")!=null?((User)request.getSession().getAttribute("user")).getUserName():null);
		model.addAllAttributes(this.commentTagsService.query(commentTags));
		return "annotations";
	}
	/**
	 * 注释查询返回数据新UI-20180503
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/annotationsN")
	@ResponseBody
	public Object queryAnnoN(CommentTags commentTags,HttpServletRequest request) {
		commentTags.setOperation(COMMENT);
		commentTags.setUserName(request.getSession().getAttribute("user")!=null?((User)request.getSession().getAttribute("user")).getUserName():null);
		return this.commentTagsService.query(commentTags);
	}
	
	/**
	 * 
	 * @param log
	 * @param model
	 * @return
	 */
	@RequestMapping("/nav-annotations")
	public String naVlog(CommentTags commentTags,Model model,HttpServletRequest request) {
		commentTags.setOperation(COMMENT);
		commentTags.setUserName(request.getSession().getAttribute("user")!=null?((User)request.getSession().getAttribute("user")).getUserName():null);

		model.addAllAttributes(this.commentTagsService.query(commentTags));
		return "nav-annotations";
	}
	@RequestMapping("/annotations/getObject")
	public String getAnno(String id,Model model,String page) {
		model.addAllAttributes(this.commentTagsService.getObject(id));
		return page;
	}
	/**
	 * 视图页面查询显示
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/annotations/view")
	@ResponseBody
	public Object  queryAnno(CommentTags commentTags,HttpServletRequest request) {
		commentTags.setOperation("commentv");
		commentTags.setUserName(request.getSession().getAttribute("user")!=null?((User)request.getSession().getAttribute("user")).getName():null);
		return this.commentTagsService.query(commentTags);
	}
	
	/**
	 * 上传附件
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/annotations/upload")
	@Operation(value="上传附件操作",type="2")
	@ResponseBody
	public Object  uploadFile(HttpServletRequest request) {
		return this.commentTagsService.uploadFile(request);
	}
	
	
	
	/**
	 * 删除附件
	 * @param commentTags
	 * @return
	 */
	@RequestMapping("/annotations/delUpload")
	@ResponseBody
	public Object  delUpload(CommentTags commentTags) {
		commentTags.setOperation("commentv");
		return this.commentTagsService.query(commentTags);
	}
	/**
	 * 书签
	 * @param commentTags
	 * @param model
	 * @return
	 */
	@RequestMapping("/tags")
	public String queryTags(CommentTags commentTags,Model model) {
		commentTags.setOperation("tag");
		model.addAllAttributes(this.commentTagsService.query(commentTags));
		return "tags";
	}
	/**
	 * 书签分页查询新UI-20180503
	 * @param commentTags
	 * @param model
	 * @return
	 */
	@RequestMapping("/tagsN")
	@ResponseBody
	public Object queryTagsN(CommentTags commentTags) {
		commentTags.setOperation("tag");
		return this.commentTagsService.query(commentTags);
	}
	
	/**
	 * 
	 * @param commentTags
	 * @param model
	 * @return
	 */
	@RequestMapping("/nav-tags")
	public String navTags(CommentTags commentTags,Model model) {
		commentTags.setOperation("tag");
		model.addAllAttributes(this.commentTagsService.query(commentTags));
		return "nav-tags";
	}
	/**
	 * 跳转到dm页面
	 */
	@RequestMapping("/goDmUrl")
	public String goDmUrl(HttpServletRequest request,Model model){
		String url=null!=request.getParameter("url")?request.getParameter("url"):"";
		String dmId=null!=request.getParameter("dmId")?request.getParameter("dmId"):"";
		String pmId=null!=request.getParameter("pmId")?request.getParameter("pmId"):"";
		String title=null!=request.getParameter("title")?request.getParameter("title"):"";
		String projectId=null!=request.getParameter(PROJECT_ID)?request.getParameter(PROJECT_ID):"";
		String requesturl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
		String userName=request.getSession().getAttribute("user")!=null?((User)request.getSession().getAttribute("user")).getUserName():null;
		model.addAttribute("requesturl", requesturl);
		model.addAttribute("userName", userName);
		model.addAttribute("url", url);
		model.addAttribute("dmId", dmId);
		model.addAttribute("pmId", pmId);
		model.addAttribute("type", request.getParameter("type"));
		model.addAttribute(PROJECT_ID, projectId);
		Date date = new Date(); // 定义时间类型
		SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String system = inputFormat.format(date);
		SysLog log = new SysLog();
		log.setOperation("查看 "+title+" ");
		log.setCreateDate(system);
		log.setUserName(userName);
		log.setResult("成功");
		log.setType("2");
		sysLogService.saveOrUpdate(log);
		return "content";
	}
	/**
	 * 保存参数到session
	 */
	@RequestMapping("/saveSession")
	@ResponseBody
	public Object saveSession(HttpServletRequest request){
		HttpSession session = request.getSession();
		if(null!=request.getParameter("type")&&"2".equals(request.getParameter("type"))){
			session.setAttribute("color", request.getParameter("color"));	//记录颜色
			session.setAttribute("font", request.getParameter("font"));	//记录字体大小
			return "";
		}
		else if(null!=request.getParameter("type")&&"1".equals(request.getParameter("type"))){
			session.setAttribute("guolvFit", request.getParameter("guolvFit"));
			return "";
		}
		return "";
	}
	/**
	 * 试题关联dm页面
	 */
	@RequestMapping("/chaptersAdd")
	public String chaptersAdd(Model model){
	/*	List<PublicModule> pms = publicModuleService.findByParentId(null);
		model.addAttribute(PMROWS, pms);*/
		return "chapters-add";
	}
}
